﻿using System;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;

using BLL;
using ExcelLibrary.SpreadSheet;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class CommentList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteSystemMaster)Master).InitMenu();
            ((SiteSystemMaster)Master).li3.Attributes.Add("class", "hover ccc");
            ((SiteSystemMaster)Master).b_hmenu1.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu3.Style.Clear();
            ((SiteSystemMaster)Master).s_menu3_3.Style.Add("COLOR", "#147ab8");
            ((SiteSystemMaster)Master).s_menu3_3.Style.Add("TEXT-DECORATION", "none"); 

            /*判断是否是管理员身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../System/Login.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        private void bind()
        {
            //if (string.IsNullOrEmpty(users_id))
            //{
            //    return;
            //}

            PositionBLL positionBll = new PositionBLL();
            DataSet dsPosition = positionBll.GetPositionAll("");
            ddlPosition.DataSource = dsPosition.Tables[0].DefaultView;
            ddlPosition.DataTextField = "position_name";
            ddlPosition.DataValueField = "position_id";

            ddlPosition.DataBind();
            //ActivityBLL temp = new ActivityBLL();
            //DataSet ds = temp.GetActivityByPositionId(ddlPosition.SelectedValue);
            CommentBLL temp = new CommentBLL();
            DataSet ds = temp.GetCommentByPosition(ddlPosition.SelectedValue);
            grVActivityManage.DataSource = ds.Tables[0].DefaultView;
            grVActivityManage.DataSourceID = null;
            grVActivityManage.DataBind();
            temp = null;
        }

        protected void ddlPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            CommentBLL temp = new  CommentBLL();
            DataSet ds = temp.GetCommentByPosition(ddlPosition.SelectedValue);

            grVActivityManage.DataSource = ds.Tables[0].DefaultView ;
            grVActivityManage.DataSourceID = null;
            grVActivityManage.DataBind();
            temp = null;
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            Boolean fileIsOK = false;
            string path = Server.MapPath("~/userfiles/");
            if (FileUploadPY.HasFile)
            {
                string fileExtension = System.IO.Path.GetExtension(FileUploadPY.FileName).ToLower();
                string[] allowExtension = { ".xls" };

                for (int i = 0; i < allowExtension.Length; i++)
                {
                    if (fileExtension == allowExtension[i])
                    {
                        fileIsOK = true;
                        break;
                    }
                }

                //Check file size < 10M
                if (FileUploadPY.PostedFile.ContentLength > 10240000)
                {
                    fileIsOK = false;
                }

                if (fileIsOK)
                {
                    try
                    {
                        FileUploadPY.PostedFile.SaveAs(path + FileUploadPY.FileName);
                        ImportComment(path + FileUploadPY.FileName);//从劲马提供的文档格式中导入
                        MessageBox.ShowAndRedirect(this, "评语导入成功：", "CommentList.aspx");
                    }
                    catch(Exception ex)
                    {
                        MessageBox.ShowAndRedirect(this, "评语导入失败：" + ex.Message, "CommentList.aspx");
                    }
                }
                else
                {
                    MessageBox.ShowAndRedirect(this, "文件大小超过10M限制", "CommentList.aspx");
                }
            }
        }

        private bool ImportComment(string xlsFileNm)
        {
            try
            {
                if (!File.Exists(xlsFileNm)) return false;

                int posID = Int32.Parse(ddlPosition.SelectedItem.Value);
                Workbook book = Workbook.Load(xlsFileNm);

                PositionBLL posBLL = new PositionBLL();
                DimensionBLL dimBLL = new DimensionBLL();
                CommentBLL comBLL = new CommentBLL();
                Comment com = new Comment();

                DataSet dimDS = dimBLL.GetDimensionAll();
                DataSet posDS = posBLL.GetPositionAll(string.Empty);

                comBLL.DelAllComment(posID);//删除与此岗位相关的所有评语。

                foreach (Worksheet sheet in book.Worksheets)
                {
                    string sheetNm = sheet.Name.Trim();
                    int dimID = -1;//在这里加断点可以监视单个维度的导入情况

                    foreach (DataRow row in dimDS.Tables[0].Rows)
                    {
                        if (row["dimension_name"].ToString().Equals(sheetNm))
                        {
                            dimID = Int32.Parse(row["dimension_id"].ToString().Trim());
                            break;
                        }
                    }
                    for (int rowIndex = sheet.Cells.FirstRowIndex + 1;//忽略第一行(标题行)
                        rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
                    {
                        Row row = sheet.Cells.GetRow(rowIndex);
                        com.Posion_id = posID;
                        com.Dimension_id = dimID;

                        string score_region = row.GetCell(0).StringValue.Trim();
                        if (score_region.Equals("偏高"))
                        {
                            com.Region_id = 1;
                        }
                        else if (score_region.Equals("适配"))
                        {
                            com.Region_id = 2;
                        }
                        else if (score_region.Equals("稍低"))
                        {
                            com.Region_id = 3;
                        }
                        else if (score_region.Equals("偏低"))
                        {
                            com.Region_id = 4;
                        }

                        for (int i = 1; i < sheet.Cells.LastColIndex + 1; i++)
                        {
                            com.Report_part_id = i;
                            com.Comment_nm = row.GetCell(i).StringValue.Trim();
                            comBLL.Add(com);
                        }
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
    }
}